//
//  QSUniversalTool.m
//  ToyTraceabilitySystem
//
//  Created by apple on 2017/3/15.
//  Copyright © 2017年 Song. All rights reserved.
//

#import "QSUniversalTool.h"
#import <UIImageView+WebCache.h>

@implementation QSUniversalTool
SingleImplementation(QSUniversalTool);

/**
 *  拨号
 */
- (void)callPhoneNumber:(NSString *)phone
{
    BOOL isPhoneNumber = [QSValidateAndPromptTool checkPhoneNumber:phone];
    
    if (isPhoneNumber) {
        NSString *phoneStr = [NSString stringWithFormat:@"tel://%@", phone];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneStr]];
    } else {
        [QSSVProgressTool showFailStatus:@"您输入的电话号码有误！"];
    }
}

/**
 *  获取当前控制器
 */
- (UIViewController *)getCurrentVC
{
    
    UIWindow *window = [[UIApplication sharedApplication].windows firstObject];
    if (!window) {
        return nil;
    }
    UIView *tempView;
    for (UIView *subview in window.subviews) {
        if ([[subview.classForCoder description] isEqualToString:@"UILayoutContainerView"]) {
            tempView = subview;
            break;
        }
    }
    if (!tempView) {
        tempView = [window.subviews lastObject];
    }
    
    id nextResponder = [tempView nextResponder];
    while (![nextResponder isKindOfClass:[UIViewController class]] || [nextResponder isKindOfClass:[UINavigationController class]] || [nextResponder isKindOfClass:[UITabBarController class]]) {
        tempView =  [tempView.subviews firstObject];
        
        if (!tempView) {
            return nil;
        }
        nextResponder = [tempView nextResponder];
    }
    return  (UIViewController *)nextResponder;
}

/**
 *  生成一个随机数
 */
- (int)getRandomNumberFrom:(int)from to:(int)to
{
    return (int)(from + (arc4random() % (to - from + 1)));
}
@end
